<?php

class Common_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }
    
    // 查询所有记录
    public function listall($table)
    {
        $query = $this->db->get($table);
        return $query->result_array();
    }
    
    // 查询所有记录个数
    public function count_all($table)
    {
        return $this->db->count_all($table);
    }
    
    // 根据id查询一条记录
    public function getone_byid($id, $table, $filed = '*')
    {
        $this->db->where('id', $id);
        $this->db->select($filed);
        $query = $this->db->get($table);
        return $query->row_array();
    }
    
    // 查询一条记录
    public function getone_where($where, $table, $filed = '*', $order = '')
    {
        $this->db->where($where);
        $this->db->select($filed);
        $this->db->limit(1);
        if ($order) {
            $this->db->order_by($order);
        }
        return $this->db->get($table)->row_array();
    }

    public function getall_where($where, $table, $filed = '*')
    {
        $this->db->where($where);
        $this->db->select($filed);
        $query = $this->db->get($table);
        return $query->result_array();
    }
    
    // 分页查询所有记录
    public function list_page($db, $where, $num, $offset, $fild = '*', $order = false)
    {
        $this->db->select($fild);
        
        $this->db->where($where);
        
        if ($order) {
            $this->db->order_by($order);
        }
        
        $query = $this->db->get($db, $num, $offset);
        return $query->result_array();
    }

    public function count_where($where, $table)
    {
        $this->db->where($where);
        
        return $this->db->count_all_results($table);
    }
    
    // 添加记录
    public function add($data, $table)
    {
        $this->db->insert($table, $data);
        return $this->db->insert_id();
    }
    
    // 根据id更新一条记录
    public function update_byid($id, $data, $table)
    {
        $this->db->where('id', $id);
        return $this->db->update($table, $data);
    }
    
    // 根据id更新一条记录
    public function update_where($where, $data, $table)
    {
        $this->db->where($where);
        return $this->db->update($table, $data);
    }
    
    // 根据id删除一条记录
    public function delete_byid($id, $table)
    {
        $this->db->where('id', $id);
        $this->db->delete($table);
    }
    
    // @todo以下为全站公用函数
    function count_by_sql($sql)
    {
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['num'];
    }

    function delete_by_sql($sql)
    {
        $query = $this->db->query($sql);
        return $query;
    }
    
    // 数据库语句
    function get_all_by_sql($sql)
    {
        $query = $this->db->query($sql);
        if ($query === true) {
            return true;
        }
        if ($query === false) {
            return false;
        } else {
            return $query->result_array();
        }
    }

    function get_one_by_sql($sql)
    {
        if (! strstr($sql, 'limit')) {
            $sql .= ' limit 1';
        }
        $query = $this->db->query($sql);
        if ($query === true) {
            return true;
        }
        if ($query === false) {
            return false;
        } else {
            return $query->row_array();
        }
    }
    
    // 根据id删除一条记录
    public function delete_where($where, $table)
    {
        $this->db->where($where);
        $this->db->delete($table);
    }

    public function getlists_by_ids($ids, $table, $fields = '*')
    {
        $query = $this->db->where_in('id', $ids)
            ->select($fields)
            ->get($table);
        $return = $query->result_array();
        $return1 = array();
        foreach ($return as $v) {
            $return1[$v['id']] = $v;
        }
        return $return1;
    }
}
?>